import 'package:flutter/material.dart';
import 'package:flutter_ncsjqz_getx/http/dio_service.dart';
import 'package:flutter_ncsjqz_getx/model/base_entity.dart';
import 'package:flutter_ncsjqz_getx/page/home/home_logic.dart';
import 'package:flutter_ncsjqz_getx/page/login/login_view.dart';
import 'package:flutter_ncsjqz_getx/page/my/message/message_view.dart';
import 'package:flutter_ncsjqz_getx/page/rong_im/rong_base_logic.dart';
import 'package:flutter_ncsjqz_getx/util/get_user_util.dart';
import 'package:flutter_ncsjqz_getx/util/toast_util.dart';
import 'package:flutter_ncsjqz_getx/widget/app_bar_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/base_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/card_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/dialog_current_project_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/ink_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/row_line_widget.dart';
import 'package:get/get.dart';

import 'email_phone_pwd/email_password_view.dart';
import 'fans_follow/fans_follow_view.dart';
import 'history_relese/history_release_view.dart';
import 'money/money_view.dart';
import 'my_logic.dart';
import 'order/order_view.dart';
import 'platform_service/platform_service_view.dart';
import 'userinfo_setting/user_info_setting_view.dart';

/// <summary>
/// author：zwb
/// dateTime： 2021/11/21 10:25
/// filePath： lib/page/my/my_view.dart
/// desc: 我的
/// <summary>
///
class MyPage extends StatefulWidget {
  @override
  _MyPageState createState() => _MyPageState();
}

class _MyPageState extends State<MyPage> {
  final logic = Get.put(MyLogic());
  List listTitle = [
    "总额","粉丝","关注"
  ];
  List<Widget> listTitlePage = [
    MoneyPage(),FansFollowPage(title: "我的粉丝",),FansFollowPage(title: "我的关注",)
  ];
  List listPayStatus = [
    "待付款","已完成","退款售后"
  ];
  List listPayIcon = [
    "dfk.png","ywc.png","tk.png"
  ];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    logic.getData();
  }

  @override
  Widget build(BuildContext context) {

    return GetBuilder<MyLogic>(
      builder: (controller) {
        return baseScaffoldWidget(
          onRefresh: () async{
            logic.getData();
          },
          child: Stack(
            children: [
              Container(
                width: Get.width,
                height: Get.height - Get.bottomBarHeight,
                color: Colors.white,
              ),

              InkWidget.imgAsset("head_bg.png",width: Get.width,fit: BoxFit.contain,),

              /// todo：顶部用户信息
              Positioned(
                top: 10,
                child: Container(
                  width: Get.width,
                  padding: EdgeInsets.all(15),
                  child: ListView(
                    physics: NeverScrollableScrollPhysics(),
                    padding: EdgeInsets.all(0),
                    shrinkWrap: true,
                    children: [

                      Container(
                        width: Get.width,
                        child: Column(
                          children: [
                            appBarRowWidget(
                              title: "我的",
                              titleColor: Colors.white,
                              backgroundColor: Colors.transparent,
                              left: SizedBox(),
                              right: Row(
                                mainAxisAlignment: MainAxisAlignment.end,
                                children: [
                                  InkWell(
                                    onTap:() {
                                      Get.to(()=>MessagePage())!.then((value) => logic.getData());
                                    },
                                    child: Stack(
                                      overflow: Overflow.visible,
                                      children: [
                                        InkWidget.imgAsset("msg.png",width: 25),
                                        Positioned(
                                          top: -10,
                                          right: -10,
                                          child: logic.msgCountData == null ? SizedBox():ClipRRect(
                                            borderRadius: BorderRadius.circular(15),
                                            child: Container(
                                              width: 20,
                                              height: 20,
                                              color: Colors.red,
                                              alignment: Alignment.center,
                                              child: InkWidget.text("${logic.msgCountData!.totalCount ?? 0}",fonSize: 14,color: Colors.white),
                                            ),
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                  SizedBox(
                                    width: 15,
                                  ),
                                  InkWell(
                                    onTap: (){
                                      Get.to(()=>UserInfoSettingPage(userDetailData:logic.userDetailData))!.then((value) => logic.getData());
                                    },
                                    child: InkWidget.imgAsset("setting.png",width: 25),
                                  ),
                                ],
                              ),
                            ),

                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                InkWell(
                                  child: SizedBox(
                                    width: (Get.width - 30) * 0.5,
                                    child: Row(
                                      //
                                      children: [
                                        ClipRRect(
                                          child: InkWidget.imgNet(
                                            "${logic.userDetailData?.userPic}",
                                            width: 65,
                                            height: 65,
                                            fit: BoxFit.cover,
                                          ),
                                          borderRadius: BorderRadius.circular(65),
                                        ),
                                        SizedBox(width: 10,),
                                        Expanded(child: Column(
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: [
                                            InkWidget.text("${logic.userDetailData?.nickName ?? ''}",height: 1.5,color: Colors.white,fonSize: 13,maxLines: 1),
                                            InkWidget.text("ID: ${logic.userDetailData?.id ?? ''}",height: 1.5,color: Colors.white,fonSize: 13,maxLines: 1),
                                            //(0-无，1-男，2-女)
                                            InkWidget.imgAsset(logic.userDetailData?.sex == 2 ?"girl.png":"man.png",width: 20),
                                          ],
                                        )),
                                      ],
                                    ),
                                  ),
                                  onTap: (){
                                    Get.to(()=>UserInfoSettingPage(userDetailData:logic.userDetailData))!.then((value) => logic.getData());
                                  },
                                ),

                                SizedBox(
                                  width: (Get.width - 30) * 0.4,
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                    children: List.generate(listTitle.length, (index){
                                      return InkWell(
                                        child: Column(
                                          children: [
                                            InkWidget.text("${logic.listCount[index] ?? 0}",color: Colors.white),
                                            InkWidget.text("${listTitle[index] ?? ''}",color: Colors.white),
                                          ],
                                        ),
                                        onTap: (){
                                          Get.to(listTitlePage[index]);
                                        },
                                      );
                                    }),
                                  ),
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),

                      /// todo：个人简介
                      cardWidget(
                        width: Get.width,
                        margin: EdgeInsets.symmetric(vertical: 5),
                        padding: EdgeInsets.all(15),
                        widget: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text("个人介绍",style: TextStyle(fontSize: 16,fontWeight: FontWeight.bold),),
                            Text(logic.userDetailData?.selfIntroduction ?? "此人很懒，暂无介绍~",overflow: TextOverflow.ellipsis,maxLines: 3,style: TextStyle(fontSize: 14,color: Colors.grey),),
                          ],
                        ),
                      ),

                      /// todo：订单
                      cardWidget(
                        widget: Column(
                          children: [
                            rowBaseWidget(leftText: "   订单管理",rightText: "全部订单",onTap: (){
                              Get.to(()=> OrderManagePage());
                            }),
                            Padding(
                              padding: EdgeInsets.all(15),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: List.generate(listPayStatus.length, (index){
                                  return Material(
                                    color: Colors.transparent,
                                    child: InkWell(
                                      child: Column(
                                        children: [
                                          InkWidget.imgAsset("${listPayIcon[index]}",width: 40,height: 40),
                                          SizedBox(height: 5,),
                                          InkWidget.text("${listPayStatus[index]}",fonSize: 14),
                                        ],
                                      ),
                                      onTap: (){
                                        Get.to(()=> OrderManagePage(currentIndex: index+1,));
                                      },
                                    ),
                                  );
                                }),
                              ),
                            ),
                          ],
                        ),
                      ),

                      rowBaseWidget(leftText: "平台客服",onTap: (){
                        Get.to(()=>PlatformServicePage());
                      }),

                      rowBaseWidget(leftText: "历史发布",onTap: (){
                        Get.to(()=>HistoryReleasePage());
                      }),

                      rowBaseWidget(leftText: "修改密码",onTap: (){
                        if(logic.userDetailData?.telPhone == null || logic.userDetailData?.telPhone == "") return toast("请先前往个人资料设置手机号");
                        Get.to(()=>EmailPhonePasswordPage(title: "修改密码",type: EPW.pwd,emailPhone: "${logic.userDetailData?.telPhone}"));
                      }),


                      rowBaseWidget(leftText: "绑定邮箱",onTap: (){
                        Get.to(()=>EmailPhonePasswordPage(title: "绑定邮箱",type: EPW.email,emailPhone: logic.userDetailData!.userMail ?? '',))!.then((value) {
                          logic.getData();
                        });
                      },showLine: false),


                      /// todo：退出按钮
                      Padding(
                        padding: EdgeInsets.symmetric(vertical: 10,horizontal: 50,),
                        child: OutlineButton(
                          onPressed: (){
                            showDialogGetx(
                              subTitle: "退出账号将不会接收到平台任何信息",
                              rowBtn: true,
                              okTap: () async{
                                // BaseEntity data = await DioService<BaseEntity>().loginOut();
                                if(0== 0){
                                  RongBaseLogic.disconnect(false);
                                  delUserUtil();
                                  HomeLogic home = Get.put(HomeLogic());
                                  home.currentPageIndex = 1;
                                  Get.off(()=>LoginPage());
                                }
                              }
                            );
                          },
                          child: Text("退出登录",style: TextStyle(color: Colors.green,),),
                          borderSide: BorderSide(color: Colors.green,width: 1.5),
                          shape: StadiumBorder(),
                        ),
                      )
                    ],
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  @override
  void dispose() {
    Get.delete<MyLogic>();
    super.dispose();
  }
}